<?php
/**
 * @user:andy
 * @date:2021/8/1
 * @company:http://www.sjdphp.com
 * @desc: 帖子监听-增删修改板块信息
 */

namespace app\admin\subscribe;
use app\common\model\Plate as PlateModel;
use app\common\model\Inv as InvModel;
use app\common\server\SystemLog;
use think\Event;

class Plate
{
    /** 新增帖子时调用
     * @param $plateId
     */
    public function onPlateAddInv($plateId){
        if(!empty($plateId)){
            try{
                ## 获取板块数据
                $plateInfo = PlateModel::where(array("plate_id" => $plateId,'plate_status' => config("project.mysql_code.table_normal")))->find();
                if(!empty($plateInfo)){
                    ## 修改板块数据
                    $plateInfo->inv_count = $plateInfo["inv_count"] + 1;
                    $plateInfo->save();
                }
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }

        }

    }

    /** 删除帖子时调用
     * @param $invId
     */
    public function onPlateDelInv($invId){
        if(!empty($invId)){
            try{
                ## 获取帖子数据
                $invInfo = InvModel::where(array("inv_id" => $invId))->find();
                if(!empty($invInfo)){
                    ## 获取板块数据
                    $plateInfo = PlateModel::where(array("plate_id" => $invInfo["plate_id"],'plate_status' => config("project.mysql_code.table_normal")))->find();
                    if(!empty($plateInfo)) {
                        $plateInfo->inv_count = $plateInfo["inv_count"] - 1;
                        $plateInfo->save();
                    }
                }
            }catch(\Exception $e){
                SystemLog::insertLog($e->getMessage());
            }
        }
    }



    public function subscribe(Event $event){
        $event->listen("addInvByPlate",[$this,'onPlateAddInv']);
        $event->listen("delInvByPlate",[$this,'onPlateDelInv']);
    }
}